var countPhotos = 0;
var starsCount = 1;

$(function() {
    createUserLineColor();
    createUserLineDoubleColor();
    getAdminMenu(sub);

    $('.tabs_sort_by ul li').on('click', function() {
        sortAdmins(this);
    });

    $('#add_new_country_save').on('click', function() {
        validateCountry();
    });

    $('#edit_country_save').on('click', function() {
        validateEditCountry();
    });

    $('#add_new_region_save').on('click', function() {
        validateRegion();
    });

    $('#edit_region_save').on('click', function() {
        validateEditRegion();
    });

    $('#add_new_city_save').on('click', function() {
        validateCity();
    });

    $('#edit_city_save').on('click', function() {
        validateEditCity();
    });

    $('#add_new_provider_save').on('click', function() {
        validateProvider();
    });

    $('#edit_provider_save').on('click', function() {
        validateEditProvider();
    });

    $('#add_new_user_agent_from').hide();
    $('#add_new_user_permissions select').change(function() {
        if ($(this).val() === 'agent') {
            $('#add_new_user_agent_from').show();
        } else {
            $('#add_new_user_agent_from').hide();
        }
    });

    $('#add_new_user_btn_save').on('click', function() {
        validUser();
    });

    if ($('#edit_user_permissions select').val() === 'agent') {
        $('#edit_user_agent_from').show();
    } else {
        $('#edit_user_agent_from').hide();
    }

    $('#edit_user_permissions select').change(function() {
        if ($(this).val() === 'agent') {
            $('#edit_user_agent_from').show();
        } else {
            $('#edit_user_agent_from').hide();
        }
    });

    $('#edit_user_btn_save').on('click', function() {
      
        validEditUser();
    });

    //Hotels

    $('.photos_container div.center_line_photo').each(function(i, v) {
        countPhotos = i + 1;
    });

    var clickcount = 0;
    $('.center_line_photo_add').on('click', function() {
        if (countPhotos < 5) {
            var input = $('<input type="file" class="upload_photo"/>');
            $('body').append(input);
            (input).click();
            addImageBlock();
        } else {
            clickcount++;
            setTimeout(function(e) {
                if (clickcount > 3) {
                    clickButtonFuck();
                }
                clickcount = 0;
            }, 1000);
        }
    });

    //delete photo
    $('.photos_container').on('click', 'div.center_line_photo_del', function() {
        $(this).parent('div.center_line_photo').remove();
        countPhotos = countPhotos - 1;
    });

    $('.field_stars ul li').each(function(i, v) {
        if ($(v).attr('selected') === 'selected') {
            starsCount = v.value;
        }
    });

    $('.field_stars ul li').mouseover(function() {
        var c = $(this).val();
        $('.field_stars ul li').each(function(i, v) {
            if (i < c) {
                $(v).children('img').attr({'src': '/travis/web/images/icoStar.png'});
            } else {
                $(v).children('img').attr({'src': '/travis/web/images/icoGreyStar.png'});
            }
        });
    }).mouseout(function() {
        var c = $(this).val();
        $('.field_stars ul li').each(function(i, v) {
            if (c !== 1) {
                if (i > c - 2) {
                    $(v).children('img').attr({'src': '/travis/web/images/icoGreyStar.png'});
                } else {
                    $(v).children('img').attr({'src': '/travis/web/images/icoStar.png'});
                }
            }
        });
    });

    $('.field_stars ul').mouseleave(function() {
        $('.field_stars ul li').each(function(i, v) {
            if (i < starsCount) {
                $(v).children('img').attr({'src': '/travis/web/images/icoStar.png'});
            } else {
                $(v).children('img').attr({'src': '/travis/web/images/icoGreyStar.png'});
            }
        });
    });

    $('.field_stars ul li').click(function() {
        var clickedStar = $(this).val();

        starsCount = clickedStar;
        $('.field_stars ul li').each(function(i, v) {
            if (i < clickedStar) {
                if ($(v).attr('selected') === 'selected') {
                    $(v).removeAttr('selected');
                }
                $(v).children('img').attr({'src': '/travis/web/images/icoStar.png'});
            } else {
                if ($(v).attr('selected') === 'selected') {
                    $(v).removeAttr('selected');
                }
                $(v).children('img').attr({'src': '/travis/web/images/icoGreyStar.png'});
            }
        });

        $(this).attr({'selected': 'selected'});
    });

    $('#add_new_hotel_save').on('click', function() {
        validHotel();
    });

    $('#edit_hotel_save').on('click', function() {
        validEditHotel();
    });

    $('#add_new_plan_save').on('click', function() {
        validPlan();
    });

    $('#edit_plan_save').on('click', function() {
        validEditPlan();
    });

    $('#add_new_room_save').on('click', function() {
        validRoom();
    });

    $('#edit_room_save').on('click', function() {
        validEditRoom();
    });

    $('#add_new_price_date input').datepicker({
        minDate: new Date(getCurrentDate()),
        dateFormat: 'dd/mm/yy'
    });

    $('#edit_price_date input').datepicker({
        minDate: new Date(getCurrentDate()),
        dateFormat: 'dd/mm/yy'
    });

    $('#add_new_price_save').on('click', function() {
        validPrice();
    });

    $('#edit_price_save').on('click', function() {
        validEditPrice();
    });
});

function getCurrentDate() {
    var d = new Date();
    var month = d.getMonth() + 1;
    var day = d.getDate();
    var output = d.getFullYear() + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            (day < 10 ? '0' : '') + day;

    return output;
}

function validEditPrice() {
    var isValid = true;

    if ($('#edit_price_hotel select').val() == 0) {
        isValid = false;
        $('#edit_price_hotel span').remove();
        $('#edit_price_hotel').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_price_hotel span').remove();
    }

    if ($('#edit_price_room select').val() == 0) {
        isValid = false;
        $('#edit_price_room span').remove();
        $('#edit_price_room').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_price_room span').remove();
    }
    //Date
    if ($('#edit_price_date input').val().length < 1) {
        isValid = false;
        $('#edit_price_date span').remove();
        $('#edit_price_date').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_price_date span').remove();
        var convertDateArr = $('#edit_price_date input').val().split("/");
        var convertDate = convertDateArr[2] + "-" + convertDateArr[1] + "-" + convertDateArr[0];
    }
    //Period
    if ($('#edit_price_period input').val().length < 1) {
        isValid = false;
        $('#edit_price_period span').remove();
        $('#edit_price_period').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_price_period span').remove();
    }
    //FIT
    if ($('#edit_price_fit input').val().length < 1 && !isValidDouble($('#edit_price_fit input').val())) {
        isValid = false;
        $('#edit_price_fit span').remove();
        $('#edit_price_fit').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_price_fit span').remove();
    }
    //CASCADE
    if ($('#edit_price_cascade input').val().length < 1 && !isValidDouble($('#edit_price_cascade input').val())) {
        isValid = false;
        $('#edit_price_cascade span').remove();
        $('#edit_price_cascade').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_price_cascade span').remove();
    }

    if ($('#edit_price_special input').val().length < 1 && !isValidDouble($('#edit_price_special input').val())) {
        isValid = false;
        $('#edit_price_special span').remove();
        $('#edit_price_special').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_price_special span').remove();
    }

    if (isValid) {
        //ajax
        $.ajax({
            type: "POST",
            url: url + "editcurrentprice",
            data: {
                id: $('#edit_price_id input').val(),
                hotel_id: $('#edit_price_hotel select').val(),
                room_id: $('#edit_price_room select').val(),
                type: $('#edit_price_type select').val(),
                date: convertDate,
                period: $('#edit_price_period input').val(),
                fit: $('#edit_price_fit input').val(),
                cascade: $('#edit_price_cascade input').val(),
                special: $('#edit_price_special input').val(),
                commission: $('#edit_commission input').val(),
                market: $('#edit_russian_market input').val()},
            success: function(msg) {
                window.location.replace(urlPrice + '?hotel_id=' + $('#edit_price_hotel select').val() + '&room_id=' + $('#edit_price_room select').val());
            }
        });
    }
}

function validPrice() {
    var isValid = true;

    if ($('#add_new_price_hotel select').val() == 0) {
        isValid = false;
        $('#add_new_price_hotel span').remove();
        $('#add_new_price_hotel').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_price_hotel span').remove();
    }

    if ($('#add_new_price_room select').val() == 0) {
        isValid = false;
        $('#add_new_price_room span').remove();
        $('#add_new_price_room').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_price_room span').remove();
    }
    //Date
    if ($('#add_new_price_date input').val().length < 1) {
        isValid = false;
        $('#add_new_price_date span').remove();
        $('#add_new_price_date').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_price_date span').remove();
        var convertDateArr = $('#add_new_price_date input').val().split("/");
        var convertDate = convertDateArr[2] + "-" + convertDateArr[1] + "-" + convertDateArr[0];
    }
    //Period
    if ($('#add_new_price_period input').val().length < 1) {
        isValid = false;
        $('#add_new_price_period span').remove();
        $('#add_new_price_period').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_price_period span').remove();
    }
    //FIT
    if ($('#add_new_price_fit input').val().length < 1 && !isValidDouble($('#add_new_price_fit input').val())) {
        isValid = false;
        $('#add_new_price_fit span').remove();
        $('#add_new_price_fit').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_price_fit span').remove();
    }
    //CASCADE
    if ($('#add_new_price_cascade input').val().length < 1 && !isValidDouble($('#add_new_price_cascade input').val())) {
        isValid = false;
        $('#add_new_price_cascade span').remove();
        $('#add_new_price_cascade').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_price_cascade span').remove();
    }

    if ($('#add_new_price_special input').val().length < 1 && !isValidDouble($('#add_new_price_special input').val())) {
        isValid = false;
        $('#add_new_price_special span').remove();
        $('#add_new_price_special').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_price_special span').remove();
    }

    if (isValid) {
        //ajax
        $.ajax({
            type: "POST",
            url: url + "addnewprice",
            data: {
                hotel_id: $('#add_new_price_hotel select').val(),
                room_id: $('#add_new_price_room select').val(),
                type: $('#add_new_price_type select').val(),
                date: convertDate,
                period: $('#add_new_price_period input').val(),
                fit: $('#add_new_price_fit input').val(),
                cascade: $('#add_new_price_cascade input').val(),
                commission: $('#add_new_commission input').val(),
                market: $('#add_new_russian_market input').val(),
                special: $('#add_new_price_special input').val()},
            success: function(msg) {
                window.location.reload(urlPrice + '?hotel_id=' + $('#add_new_price_hotel select').val() + '&room_id=' + $('#add_new_price_room select').val());
            }
        });
    }
}

function validEditRoom() {
    var isValid = true;

    if ($('#edit_room_name input').val().length < 3) {
        isValid = false;
        $('#edit_room_name span').remove();
        $('#edit_room_name').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_room_name span').remove();
    }

    if (isValid) {
        //ajax
        $.ajax({
            type: "POST",
            url: url + "editcurrentroom",
            data: {
                id: $('#edit_room_id input').val(),
                hotel_id: $('#edit_room_hotel select').val(),
                name: $('#edit_room_name input').val(),
                code: $('#edit_room_code input').val(),
                description_en: $('#edit_room_descripton_en textarea').val(),
                description_ru: $('#edit_room_descripton_ru textarea').val(),
                description_he: $('#edit_room_descripton_he textarea').val(),
                adult: $('#edit_room_adult select').val(),
                children: $('#edit_room_children select').val(),
                baby: $('#edit_room_baby select').val()},
            success: function(msg) {
                window.location.replace(urlHotelDetails + '/id/' + $('#edit_room_hotel select').val());
            }
        });
    }
}

function validRoom() {
    var isValid = true;

    if ($('#add_new_room_name input').val().length < 3) {
        isValid = false;
        $('#add_new_room_name span').remove();
        $('#add_new_room_name').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_room_name span').remove();
    }

    if (isValid) {
        //ajax
        $.ajax({
            type: "POST",
            url: url + "addnewroom",
            data: {
                hotel_id: $('#add_new_room_hotel select').val(),
                name: $('#add_new_room_name input').val(),
                code: $('#add_new_room_code input').val(),
                description_en: $('#add_new_room_descripton_en textarea').val(),
                description_ru: $('#add_new_room_descripton_ru textarea').val(),
                description_he: $('#add_new_room_descripton_he textarea').val(),
                adult: $('#add_new_room_adult select').val(),
                children: $('#add_new_room_children select').val(),
                baby: $('#add_new_room_baby select').val()},
            success: function(msg) {
//                console.log(urlRooms + '?hotel_id=' + $('#add_new_room_hotel select').val());
                window.location.replace(urlHotelDetails + '/id/' + $('#add_new_room_hotel select').val());
            }
        });
    }
}

function validEditPlan() {
    var isValid = true;

    if ($('#edit_plan_en input').val().length < 3) {
        isValid = false;
        $('#edit_plan_en span').remove();
        $('#edit_plan_en').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_plan_en span').remove();
    }

    if (isValid) {
        //ajax
        $.ajax({
            type: "POST",
            url: url + "editcurrentplan",
            data: {
                id: $('#edit_plan_id input').val(),
                name_en: $('#edit_plan_en input').val(),
                name_ru: $('#edit_plan_ru input').val(),
                name_he: $('#edit_plan_he input').val()},
            success: function(msg) {
                window.location.replace(urlPlans);
            }
        });
    }
}

function validPlan() {
    var isValid = true;

    if ($('#add_new_plan_en input').val().length < 3) {
        isValid = false;
        $('#add_new_plan_en span').remove();
        $('#add_new_plan_en').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_plan_en span').remove();
    }

    if (isValid) {
        //ajax
        $.ajax({
            type: "POST",
            url: url + "addnewplan",
            data: {
                name_en: $('#add_new_plan_en input').val(),
                name_ru: $('#add_new_plan_ru input').val(),
                name_he: $('#add_new_plan_he input').val()},
            success: function(msg) {
                window.location.replace(urlPlans);
            }
        });
    }
}

function validEditHotel() {
    var isValid = true;
    var imagesBase64 = [];
    var stars = 1;

    $('#edit_hotel_stars ul li').each(function(i, v) {
        if ($(v).attr('selected') === 'selected') {
            stars = v.value;
        }
    });

    $('.photos_container div img').each(function(i, v) {
        console.log(v);
//        var image = $(v).attr('src').split(',');
//        image = image[0].split(':');
//        if (image[0] == 'data') {
        imagesBase64.push($(v).attr('src'));
//        }
    });

    if ($('#edit_hotel_en input').val().length < 3) {
        isValid = false;
        $('#edit_hotel_en span').remove();
        $('#edit_hotel_en').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_hotel_en span').remove();
    }

    if ($('#edit_hotel_ru input').val().length < 3) {
        isValid = false;
        $('#edit_hotel_ru span').remove();
        $('#edit_hotel_ru').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_hotel_ru span').remove();
    }

    if ($('#edit_hotel_he input').val().length < 3) {
        isValid = false;
        $('#edit_hotel_he span').remove();
        $('#edit_hotel_he').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_hotel_he span').remove();
    }

    if ($('#edit_hotel_country select').val() == 0) {
        isValid = false;
        $('#edit_hotel_country span').remove();
        $('#edit_hotel_country').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_hotel_country span').remove();
    }

    if ($('#edit_hotel_region select').val() == 0) {
        isValid = false;
        $('#edit_hotel_region span').remove();
        $('#edit_hotel_region').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_hotel_region span').remove();
    }

    if ($('#edit_hotel_city select').val() == 0) {
        isValid = false;
        $('#edit_hotel_city span').remove();
        $('#edit_hotel_city').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_hotel_city span').remove();
    }

    if ($('#edit_hotel_provider select').val() == 0) {
        isValid = false;
        $('#edit_hotel_provider span').remove();
        $('#edit_hotel_provider').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_hotel_provider span').remove();
    }

    if ($('#edit_hotel_address input').val().length < 3) {
        isValid = false;
        $('#edit_hotel_address span').remove();
        $('#edit_hotel_address').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_hotel_address span').remove();
    }

    if ($('#edit_hotel_phone1 input').val().length < 3) {
        isValid = false;
        $('#edit_hotel_phone1 span').remove();
        $('#edit_hotel_phone1').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_hotel_phone1 span').remove();
    }

    if (isValid) {
        //ajax
        $.ajax({
            type: "POST",
            url: url + "editcurrenthotel",
            data: {
                id: $('#edit_hotel_id input').val(),
                name_en: $('#edit_hotel_en input').val(),
                name_ru: $('#edit_hotel_ru input').val(),
                name_he: $('#edit_hotel_he input').val(),
                stars: stars,
                country: $('#edit_hotel_country select').val(),
                region: $('#edit_hotel_region select').val(),
                city: $('#edit_hotel_city select').val(),
                provider: $('#edit_hotel_provider select').val(),
                address: $('#edit_hotel_address input').val(),
                phone1: $('#edit_hotel_phone1 input').val(),
                phone2: $('#edit_hotel_phone2 input').val(),
                fax: $('#edit_hotel_fax input').val(),
                email: $('#edit_hotel_email input').val(),
                web: $('#edit_hotel_web input').val(),
                description_en: $('#edit_hotel_descripton_en textarea').val(),
                description_ru: $('#edit_hotel_descripton_ru textarea').val(),
                description_he: $('#edit_hotel_descripton_he textarea').val(),
                priority: $('#edit_hotel_priority select').val(),
                images: imagesBase64},
            beforeSend: function() {

            },
            success: function(msg) {
                window.location.assign(urlHotelDetails + '/id/' + $('#edit_hotel_id input').val());
            }
        });
    }
}

function validHotel() {
    var isValid = true;
    var imagesBase64 = [];
    var stars = 1;

    $('#add_new_hotel_stars ul li').each(function(i, v) {
        if ($(v).attr('selected') === 'selected') {
            stars = v.value;
        }
    });

    $('.photos_container div img').each(function(i, v) {
        var image = $(v).attr('src').split(',');
        image = image[0].split(':');
        if (image[0] == 'data') {
            imagesBase64.push($(v).attr('src'));
        }
    });

    if ($('#add_new_hotel_en input').val().length < 3) {
        isValid = false;
        $('#add_new_hotel_en span').remove();
        $('#add_new_hotel_en').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_hotel_en span').remove();
    }

    if ($('#add_new_hotel_ru input').val().length < 3) {
        isValid = false;
        $('#add_new_hotel_ru span').remove();
        $('#add_new_hotel_ru').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_hotel_ru span').remove();
    }

    if ($('#add_new_hotel_he input').val().length < 3) {
        isValid = false;
        $('#add_new_hotel_he span').remove();
        $('#add_new_hotel_he').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_hotel_he span').remove();
    }

    if ($('#add_new_hotel_country select').val() == 0) {
        isValid = false;
        $('#add_new_hotel_country span').remove();
        $('#add_new_hotel_country').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_hotel_country span').remove();
    }

    if ($('#add_new_hotel_region select').val() == 0) {
        isValid = false;
        $('#add_new_hotel_region span').remove();
        $('#add_new_hotel_region').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_hotel_region span').remove();
    }

    if ($('#add_new_hotel_city select').val() == 0) {
        isValid = false;
        $('#add_new_hotel_city span').remove();
        $('#add_new_hotel_city').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_hotel_city span').remove();
    }

    if ($('#add_new_hotel_provider select').val() == 0) {
        isValid = false;
        $('#add_new_hotel_provider span').remove();
        $('#add_new_hotel_provider').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_hotel_provider span').remove();
    }

    if ($('#add_new_hotel_address input').val().length < 3) {
        isValid = false;
        $('#add_new_hotel_address span').remove();
        $('#add_new_hotel_address').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_hotel_address span').remove();
    }

    if ($('#add_new_hotel_phone1 input').val().length < 3) {
        isValid = false;
        $('#add_new_hotel_phone1 span').remove();
        $('#add_new_hotel_phone1').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_hotel_phone1 span').remove();
    }

    if (isValid) {
        //ajax
        $.ajax({
            type: "POST",
            url: url + "addnewhotel",
            data: {
                name_en: $('#add_new_hotel_en input').val(),
                name_ru: $('#add_new_hotel_ru input').val(),
                name_he: $('#add_new_hotel_he input').val(),
                stars: stars,
                country: $('#add_new_hotel_country select').val(),
                region: $('#add_new_hotel_region select').val(),
                city: $('#add_new_hotel_city select').val(),
                provider: $('#add_new_hotel_provider select').val(),
                address: $('#add_new_hotel_address input').val(),
                phone1: $('#add_new_hotel_phone1 input').val(),
                phone2: $('#add_new_hotel_phone2 input').val(),
                fax: $('#add_new_hotel_fax input').val(),
                email: $('#add_new_hotel_email input').val(),
                web: $('#add_new_hotel_web').val(),
                description_en: $('#add_new_hotel_descripton_en textarea').val(),
                description_ru: $('#add_new_hotel_descripton_ru textarea').val(),
                description_he: $('#add_new_hotel_descripton_he textarea').val(),
                priority: $('#add_new_hotel_priority select').val(),
                images: imagesBase64},
            beforeSend: function() {
//                showLoader();
            },
            success: function(msg) {
//                hideLoader();
                window.location.assign(urlHotel);
//                var json = $.parseJSON(msg);
//                $(json).each(function(i, v) {
//                    if (v.status == '1') {
//                        window.location.replace(urlHotel);
//                    } else {
//                        
//                    }
//                });
            }
        });
    }
}

function clickButtonFuck() {
    $('.center_line_photo_add').css({
        background: 'url(/travis/web/images/clicked4.gif) center no-repeat'
    });

    setTimeout(function() {
        $('.center_line_photo_add').css({
            background: 'url(/travis/web/images/addImage.png) center no-repeat'
        });
    }, 4000);
}

function addImageBlock() {
    $('.upload_photo').change(function() {
        if (this.files && this.files[0]) {
            countPhotos = countPhotos + 1;
            var reader = new FileReader();

            reader.onload = function(e) {
                newImageBlock(e.target.result);
            };

            reader.readAsDataURL(this.files[0]);
        }
    });
}

function newImageBlock(image) {
    var div = $('.photos_container');
    var divMain = '<div class="center_line_photo">';
    var divDelete = '<div class="center_line_photo_del"></div>';
    var imgPhoto = '<img class="image_hotel" src="' + image + '">';

    (div).append($(divMain).append(divDelete).append(imgPhoto));

    var deleteInput = $('body').find('upload_photo');
    (deleteInput).remove();
}

function getAdminMenu(menu) {
    var selMenu = $('.menu_main_admin ul li a').find('active');
    selMenu.removeClass('active');

    $('.menu_main_admin ul li a').each(function(i, v) {
        if (menu === $(v).text()) {
            $(this).addClass('active');
        }
    });
}

function validUser() {
    var isValid = true;

    if (!isValidFirstName($('#add_new_user_first_name input').val())) {
        isValid = false;
        $('#add_new_user_first_name span').remove();
        $('#add_new_user_first_name').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_user_first_name span').remove();
    }

    if (!isValidLastName($('#add_new_user_last_name input').val())) {
        isValid = false;
        $('#add_new_user_last_name span').remove();
        $('#add_new_user_last_name').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_user_last_name span').remove();
    }

    if (!isValidEmailAddress($('#add_new_user_email input').val())) {
        isValid = false;
        $('#add_new_user_email span').remove();
        $('#add_new_user_email').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_user_email span').remove();
    }

//    if (!isValidPassword($('#add_new_user_password input').val(), $('#add_new_user_vpassword input').val())) {
//        isValid = false;
//        $('#add_new_user_password span').remove();
//        $('#add_new_user_vpassword span').remove();
//        $('#add_new_user_password').append('<span class="field_error">Error</span>');
//    } else {
//        $('#add_new_user_password span').remove();
//        $('#add_new_user_vpassword span').remove();
//    }

    if (!isValidPhone($('#add_new_user_phone1 input').val())) {
        isValid = false;
        $('#add_new_user_phone1 span').remove();
        $('#add_new_user_phone1').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_user_phone1 span').remove();
    }

    if ($('#add_new_user_country select').val() === 0) {
        isValid = false;
        $('#add_new_user_country span').remove();
        $('#add_new_user_country').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_user_country span').remove();
    }

    if ($('#add_new_user_city select').val() === 0) {
        isValid = false;
        $('#add_new_user_city span').remove();
        $('#add_new_user_city').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_user_city span').remove();
    }

    if (isValid) {
        //ajax
        $.ajax({
            type: "POST",
            url: url + "addnewuser",
            data: {
                title: $('#add_new_user_title input').val(),
                first_name: $('#add_new_user_first_name input').val(),
                last_name: $('#add_new_user_last_name input').val(),
                email: $('#add_new_user_email input').val(),
                password: $('#add_new_user_password input').val(),
                phone1: $('#add_new_user_phone1 input').val(),
                phone2: $('#add_new_user_phone2 input').val(),
                fax: $('#add_new_user_fax input').val(),
                country: $('#add_new_user_country select').val(),
                city: $('#add_new_user_city select').val(),
                address: $('#add_new_user_address input').val(),
                permissions: $('#add_new_user_permissions select').val(),
                agent: $('#add_new_user_agent_from select').val(),
                status: $('#add_new_user_status select').val(),
                commissions: $('#add_new_user_commisions input').val(),
                lat:$('#lat').val(),
                lng:$('#lng').val(),
            },
            success: function() {
                window.location.replace(urlUser);
            }
        });
    }
}

function validEditUser() {
    var isValid = true;

    if (!isValidFirstName($('#edit_user_first_name input').val())) {
        isValid = false;
        $('#edit_user_first_name span').remove();
        $('#edit_user_first_name').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_user_first_name span').remove();
    }

    if (!isValidLastName($('#edit_user_last_name input').val())) {
        isValid = false;
        $('#edit_user_last_name span').remove();
        $('#edit_user_last_name').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_user_last_name span').remove();
    }

    if (!isValidEmailAddress($('#edit_user_email input').val())) {
        isValid = false;
        $('#edit_user_email span').remove();
        $('#edit_user_email').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_user_email span').remove();
    }

//    if (!isValidPassword($('#edit_user_password input').val(), $('#edit_user_vpassword input').val())) {
//        isValid = false;
//        $('#edit_user_password span').remove();
//        $('#edit_user_vpassword span').remove();
//        $('#edit_user_password').append('<span class="field_error">Error</span>');
//    } else {
//        $('#edit_user_password span').remove();
//        $('#add_new_user_vpassword span').remove();
//    }

    if (!isValidPhone($('#edit_user_phone1 input').val())) {
        isValid = false;
        $('#edit_user_phone1 span').remove();
        $('#edit_user_phone1').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_user_phone1 span').remove();
    }

    if ($('#edit_user_country select').val() === 0) {
        isValid = false;
        $('#edit_user_country span').remove();
        $('#edit_user_country').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_user_country span').remove();
    }

    if ($('#edit_user_city select').val() === 0) {
        isValid = false;
        $('#edit_user_city span').remove();
        $('#edit_user_city').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_user_city span').remove();
    }

    if (isValid) {
        //ajax
        $.ajax({
            type: "POST",
            url: url + "editcurrentuser",
            data: {
                id: $('#edit_user_id input').val(),
                title: $('#edit_user_title input').val(),
                first_name: $('#edit_user_first_name input').val(),
                last_name: $('#edit_user_last_name input').val(),
                email: $('#edit_user_email input').val(),
                password: $('#edit_user_password input').val(),
                phone1: $('#edit_user_phone1 input').val(),
                phone2: $('#edit_user_phone2 input').val(),
                fax: $('#edit_user_fax input').val(),
                country: $('#edit_user_country select').val(),
                city: $('#edit_user_city select').val(),
                address: $('#edit_user_address input').val(),
                permissions: $('#edit_user_permissions select').val(),
                agent: $('#edit_user_agent_from select').val(),
                commissions: $('#edit_user_commisions input').val(),
                status: $('#edit_user_status select').val(),
                lat:$('#lat').val(),
                lng:$('#lng').val()
                
            },
             
            success: function() {
                window.location.replace(urlUser);
            }
        });
    }
}

function validateEditProvider() {
    var isValid = true;

    if ($('#edit_provider_name input').val().length < 3) {
        isValid = false;
        $('#edit_provider_name span').remove();
        $('#edit_provider_name').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_provider_name span').remove();
    }

    if ($('#edit_provider_short_name input').val().length < 3) {
        isValid = false;
        $('#edit_provider_short_name span').remove();
        $('#edit_provider_short_name').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_provider_short_name span').remove();
    }

    if (isValid) {
        //ajax
        $.ajax({
            type: "POST",
            url: url + "editcurrentprovider",
            data: {
                id: $('#edit_provider_id input').val(),
                name: $('#edit_provider_name input').val(),
                short: $('#edit_provider_short_name input').val(),
                username: $('#edit_provider_username input').val(),
                pass: $('#edit_provider_pass input').val()},
            success: function() {
                window.location.replace(urlPovider);
            }
        });
    }
}

function validateProvider() {
    var isValid = true;

    if ($('#add_new_provider_name input').val().length < 3) {
        isValid = false;
        $('#add_new_provider_name span').remove();
        $('#add_new_provider_name').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_provider_name span').remove();
    }

    if ($('#add_new_provider_short_name input').val().length < 3) {
        isValid = false;
        $('#add_new_provider_short_name span').remove();
        $('#add_new_provider_short_name').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_provider_short_name span').remove();
    }

    if (isValid) {
        //ajax
        $.ajax({
            type: "POST",
            url: url + "addnewprovider",
            data: {
                name: $('#add_new_provider_name input').val(),
                short: $('#add_new_provider_short_name input').val(),
                username: $('#add_new_provider_username input').val(),
                pass: $('#add_new_provider_pass input').val()},
            success: function() {
                window.location.replace(urlPovider);
            }
        });
    }
}

function validateEditCity() {
    var isValid = true;

    if ($('#edit_city_en input').val().length < 3) {
        isValid = false;
        $('#edit_city_en span').remove();
        $('#edit_city_en').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_city_en span').remove();
    }

    if ($('#edit_city_ru input').val().length < 3) {
        isValid = false;
        $('#edit_city_ru span').remove();
        $('#edit_city_ru').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_city_ru span').remove();
    }

    if ($('#edit_city_he input').val().length < 3) {
        isValid = false;
        $('#edit_city_he span').remove();
        $('#edit_city_he').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_city_he span').remove();
    }

    if (isValid) {
        //ajax
        $.ajax({
            type: "POST",
            url: url + "editcurrentcity",
            data: {
                country: $('#edit_city_country select').val(),
                region: $('#edit_city_region select').val(),
                priority: $('#edit_city_priority select').val(),
                en: $('#edit_city_en input').val(),
                he: $('#edit_city_he input').val(),
                ru: $('#edit_city_ru input').val(),
                id: $('#edit_city_id input').val()},
            success: function() {
                window.location.replace(urlCity);
            }
        });
    }
}

function validateCity() {
    var isValid = true;

    if ($('#add_new_city_en input').val().length < 3) {
        isValid = false;
        $('#add_new_city_en span').remove();
        $('#add_new_city_en').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_city_en span').remove();
    }

    if ($('#add_new_city_ru input').val().length < 3) {
        isValid = false;
        $('#add_new_city_ru span').remove();
        $('#add_new_city_ru').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_city_ru span').remove();
    }

    if ($('#add_new_city_he input').val().length < 3) {
        isValid = false;
        $('#add_new_city_he span').remove();
        $('#add_new_city_he').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_city_he span').remove();
    }

    if (isValid) {
        //ajax
        $.ajax({
            type: "POST",
            url: url + "addnewcity",
            data: {
                country: $('#add_new_city_country select').val(),
                region: $('#add_new_city_region select').val(),
                priority: $('#add_new_city_priority select').val(),
                en: $('#add_new_city_en input').val(),
                he: $('#add_new_city_he input').val(),
                ru: $('#add_new_city_ru input').val()},
            success: function() {
                window.location.replace(urlCity);
            }
        });
    }
}

function validateEditRegion() {
    var isValid = true;

    if ($('#edit_region_en input').val().length < 3) {
        isValid = false;
        $('#edit_region_en span').remove();
        $('#edit_region_en').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_region_en span').remove();
    }

    if ($('#edit_region_ru input').val().length < 3) {
        isValid = false;
        $('#edit_region_ru span').remove();
        $('#edit_region_ru').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_region_ru span').remove();
    }

    if ($('#edit_region_he input').val().length < 3) {
        isValid = false;
        $('#edit_region_he span').remove();
        $('#edit_region_he').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_region_he span').remove();
    }

    if (isValid) {
        //ajax
        $.ajax({
            type: "POST",
            url: url + "editcurrentregion",
            data: {id: $('#edit_region_id input').val(), country: $('#edit_region_country select').val(), en: $('#edit_region_en input').val(), he: $('#edit_region_he input').val(), ru: $('#edit_region_ru input').val()},
            success: function() {
                window.location.replace(urlRegion);
            }
        });
    }
}

function validateRegion() {
    var isValid = true;

    if ($('#add_new_region_en input').val().length < 3) {
        isValid = false;
        $('#add_new_region_en span').remove();
        $('#add_new_region_en').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_region_en span').remove();
    }

    if ($('#add_new_region_ru input').val().length < 3) {
        isValid = false;
        $('#add_new_region_ru span').remove();
        $('#add_new_region_ru').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_region_ru span').remove();
    }

    if ($('#add_new_region_he input').val().length < 3) {
        isValid = false;
        $('#add_new_region_he span').remove();
        $('#add_new_region_he').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_region_he span').remove();
    }

    if (isValid) {
        //ajax
        $.ajax({
            type: "POST",
            url: url + "addnewregion",
            data: {country: $('#add_new_region_country select').val(), en: $('#add_new_region_en input').val(), he: $('#add_new_region_he input').val(), ru: $('#add_new_region_ru input').val()},
            success: function() {
                window.location.replace(urlRegion);
            }
        });
    }
}

function validateEditCountry() {
    var isValid = true;

    if ($('#edit_country_en input').val().length < 3) {
        isValid = false;
        $('#edit_country_en span').remove();
        $('#edit_country_en').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_country_en span').remove();
    }

    if ($('#edit_country_he input').val().length < 3) {
        isValid = false;
        $('#edit_country_he span').remove();
        $('#edit_country_he').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_country_he span').remove();
    }

    if ($('#edit_country_ru input').val().length < 3) {
        isValid = false;
        $('#edit_country_ru span').remove();
        $('#edit_country_ru').append('<span class="field_error">Error</span>');
    } else {
        $('#edit_country_ru span').remove();
    }

    if (isValid) {
        //ajax
        $.ajax({
            type: "POST",
            url: url + "editcurrentcountry",
            data: {id: $('#edit_country_id input').val(), en: $('#edit_country_en input').val(), he: $('#edit_country_he input').val(), ru: $('#edit_country_ru input').val()},
            success: function() {
                window.location.replace(urlCountry);
            }
        });
    }
}

function validateCountry() {
    var isValid = true;

    if ($('#add_new_country_en input').val().length < 3) {
        isValid = false;
        $('#add_new_country_en span').remove();
        $('#add_new_country_en').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_country_en span').remove();
    }

    if ($('#add_new_country_he input').val().length < 3) {
        isValid = false;
        $('#add_new_country_he span').remove();
        $('#add_new_country_he').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_country_he span').remove();
    }

    if ($('#add_new_country_ru input').val().length < 3) {
        isValid = false;
        $('#add_new_country_ru span').remove();
        $('#add_new_country_ru').append('<span class="field_error">Error</span>');
    } else {
        $('#add_new_country_ru span').remove();
    }

    if (isValid) {
        //ajax
        $.ajax({
            type: "POST",
            url: url + "addnewcountry",
            data: {en: $('#add_new_country_en input').val(), he: $('#add_new_country_he input').val(), ru: $('#add_new_country_ru input').val()},
            success: function() {
                window.location.replace(urlCountry);
            }
        });
    }
}

function sortAdmins(sort) {
    $('.tabs_sort_by ul li').each(function(i, v) {
        if ($(v).attr('class') === 'active') {
            $(v).removeClass('active');
        }

        if ($(v).text() === $(sort).text()) {
            $(v).addClass('active');
        }
    });
}

function createUserLineColor() {
    $('.user_line').each(function(i, v) {
        if (i % 2 === 0) {
            $(this).css({
                'background-color': '#ededed'
            });
        } else {
            $(this).css({
                'background-color': '#ffffff'
            });
        }
    });
}

function createUserLineDoubleColor() {
    $('.user_line_double').each(function(i, v) {
        if (i % 2 === 0) {
            $(this).css({
                'background-color': '#ededed'
            });
        } else {
            $(this).css({
                'background-color': '#ffffff'
            });
        }
    });
}

function showLoader() {
    var winWidth = $(window).width();
    var winHeight = $(window).height();

    $('#blackout').css({'width': winWidth + 'px', 'height': winHeight + 'px'});
    $('#blackout').show();

    var boxWidth = 220;
    var disWidth = (winWidth - boxWidth) / 2;
    var disHeight = 200;
    $('.loader').css({'width': boxWidth + 'px', 'left': disWidth + 'px', 'top': disHeight + 'px'});
    $('.loader').show();
}

function hideLoader() {
    $('#blackout').css({'width': 0 + 'px', 'height': 0 + 'px'});
    $('#blackout').hide();

    $('.loader').hide();
}
function gmap_show_address(lat,lng,content)
{
     var loc = new google.maps.LatLng(lat, lng);
          
         
           var mapOptions = {
               center: loc,
               zoom:12,
               mapTypeId: google.maps.MapTypeId.ROADMAP
            };
           var map = new google.maps.Map(document.getElementById("agents_map"), mapOptions);
            
          
           var marker = new google.maps.Marker({
                position: loc,
                map: map,
                title:content
            });
            
            var infowindow = new google.maps.InfoWindow({
                content:content
            });
               
            google.maps.event.addListener(marker, 'click', function() {
             infowindow.open(map,marker);
           });
}
function find_agent_address(part,flg)
{
    if(flg==undefined)flg=false;
    var lat;
    var lng;
     var addr='';
      addr+=$(part+'user_address .field_input').val()+' ,';
      addr+=$(part+'user_city .field_select :selected').text()+' ,';
      addr+=$(part+'user_country .field_select :selected').text();
      
    if(($('#lat').val().length>0)&&($('#lng').val().length>0)&&(flg))
    {
      lat=$('#lat').val();
      lng=$('#lng').val();
      
      gmap_show_address(lat,lng,addr);
    }else{
     
     
      $.ajax(
        {
        url: 'http://maps.googleapis.com/maps/api/geocode/json',
        data: 'address=' + addr + '&sensor=true',
        dataType: 'json',
        cache: false,
        success: function(data)
        { 
            if(data.status=='OK')
            {
             lat=data.results[0].geometry.location.lat;
             lng=data.results[0].geometry.location.lng;
            $('#lat').val(lat);
            $('#lng').val(lng);
            gmap_show_address(lat,lng,data.results[0].formatted_address);
            }
          
        }
        });
    }    
}
